<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<HTML>
<HEAD>
<LINK REL=STYLESHEET TYPE="text/css" HREF="stylesheet.css" TITLE="Style">
<META NAME="GENERATOR" CONTENT="Java2HTML Version 1.3.1">
<TITLE>jena.examples.ontology.describeClass.DescribeClass (Java2HTML)</TITLE>
</HEAD>
<BODY><TABLE id="Header" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td colspan="2" width="33%">&nbsp;</td>
<td align="center" colspan="2" width="33%">
<font size="4">DescribeClass.java</font>
</td>
<td align="right" colspan="2" width="33%">&nbsp;</td>
</tr>
</TABLE>
<pre ID="Classes">
<A NAME="1"></A><FONT ID="FormalComment">/*****************************************************************************
<A NAME="2"></A> * Source code information
<A NAME="3"></A> * -----------------------
<A NAME="4"></A> * Original author    Ian Dickinson, HP Labs Bristol
<A NAME="5"></A> * Author email       ian.dickinson@hp.com
<A NAME="6"></A> * Package            Jena 2
<A NAME="7"></A> * Web                http://sourceforge.net/projects/jena/
<A NAME="8"></A> * Created            25-Jul-2003
<A NAME="9"></A> * Filename           $RCSfile: DescribeClass.java.html,v $
<A NAME="10"></A> * Revision           $Revision: 1.3 $
<A NAME="11"></A> * Release status     $State: Exp $
<A NAME="12"></A> *
<A NAME="13"></A> * Last modified on   $Date: 2007/01/17 10:39:43 $
<A NAME="14"></A> *               by   $Author: andy_seaborne $
<A NAME="15"></A> *
<A NAME="16"></A> * (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
<A NAME="17"></A> * (see footer for full conditions)
<A NAME="18"></A> *****************************************************************************/</FONT>
<A NAME="19"></A>
<A NAME="20"></A><FONT ID="SingleLineComment">// Package
<A NAME="21"></A></FONT><FONT ID="SingleLineComment">///////////////
<A NAME="22"></A></FONT><FONT ID="Package">package</FONT> jena.examples.ontology.describeClass;
<A NAME="23"></A>
<A NAME="24"></A><FONT ID="SingleLineComment">// Imports
<A NAME="25"></A></FONT><FONT ID="SingleLineComment">///////////////
<A NAME="26"></A></FONT><FONT ID="Import">import</FONT> java.io.PrintStream;
<A NAME="27"></A><FONT ID="Import">import</FONT> java.util.*;
<A NAME="28"></A>
<A NAME="29"></A><FONT ID="Import">import</FONT> com.hp.hpl.jena.ontology.*;
<A NAME="30"></A><FONT ID="Import">import</FONT> com.hp.hpl.jena.rdf.model.*;
<A NAME="31"></A><FONT ID="Import">import</FONT> com.hp.hpl.jena.shared.PrefixMapping;
<A NAME="32"></A>
<A NAME="33"></A>
<A NAME="34"></A>
<A NAME="35"></A><FONT ID="FormalComment">/**
<A NAME="36"></A> * &lt;p&gt;
<A NAME="37"></A> * Simple example of describing the basic attributes of a OWL, DAML or RDFS class
<A NAME="38"></A> * using the ontology API.  This is not meant as a definitive solution to the problem,
<A NAME="39"></A> * but as an illustration of one approach to solving the problem. This example should
<A NAME="40"></A> * be adapted as necessary to provide a given application with the means to render
<A NAME="41"></A> * a class description in a readable form.
<A NAME="42"></A> * &lt;/p&gt;
<A NAME="43"></A> *
<A NAME="44"></A> * @author Ian Dickinson, HP Labs
<A NAME="45"></A> *         (&lt;a  href="mailto:Ian.Dickinson@hp.com" &gt;email&lt;/a&gt;)
<A NAME="46"></A> * @version CVS $Id: DescribeClass.java.html,v 1.3 2007/01/17 10:39:43 andy_seaborne Exp $
<A NAME="47"></A> */</FONT>
<A NAME="48"></A><FONT ID="Public">public</FONT> <FONT ID="Class">class</FONT> DescribeClass {
<A NAME="49"></A>    <FONT ID="SingleLineComment">// Constants
<A NAME="50"></A></FONT>    <FONT ID="SingleLineComment">//////////////////////////////////
<A NAME="51"></A></FONT>
<A NAME="52"></A>
<A NAME="53"></A>
<A NAME="54"></A>    <FONT ID="SingleLineComment">// Static variables
<A NAME="55"></A></FONT>    <FONT ID="SingleLineComment">//////////////////////////////////
<A NAME="56"></A></FONT>
<A NAME="57"></A>    <FONT ID="SingleLineComment">// Instance variables
<A NAME="58"></A></FONT>    <FONT ID="SingleLineComment">//////////////////////////////////
<A NAME="59"></A></FONT>
<A NAME="60"></A>    <FONT ID="Private">private</FONT> Map m_anonIDs = <FONT ID="New">new</FONT> HashMap();
<A NAME="61"></A>    <FONT ID="Private">private</FONT> <FONT ID="Int">int</FONT> m_anonCount = <FONT ID="IntegerLiteral">0</FONT>;
<A NAME="62"></A>
<A NAME="63"></A>
<A NAME="64"></A>    <FONT ID="SingleLineComment">// Constructors
<A NAME="65"></A></FONT>    <FONT ID="SingleLineComment">//////////////////////////////////
<A NAME="66"></A></FONT>
<A NAME="67"></A>    <FONT ID="SingleLineComment">// External signature methods
<A NAME="68"></A></FONT>    <FONT ID="SingleLineComment">//////////////////////////////////
<A NAME="69"></A></FONT>
<A NAME="70"></A>    <FONT ID="FormalComment">/**
<A NAME="71"></A>     * &lt;p&gt;Describe the given ontology class in texttual form. The description
<A NAME="72"></A>     * produced has the following form (approximately):
<A NAME="73"></A>     * &lt;pre&gt;
<A NAME="74"></A>     * Class foo:Bar
<A NAME="75"></A>     *    is a sub-class of foo:A, ex:B
<A NAME="76"></A>     *    is a super-class of ex:C
<A NAME="77"></A>     * &lt;/pre&gt;
<A NAME="78"></A>     * &lt;/p&gt;
<A NAME="79"></A>     *
<A NAME="80"></A>     * @param out The print stream to write the description to
<A NAME="81"></A>     * @param cls The ontology class to describe
<A NAME="82"></A>     */</FONT>
<A NAME="83"></A>    <FONT ID="Public">public</FONT> <FONT ID="Void">void</FONT> describeClass( PrintStream out, OntClass cls ) {
<A NAME="84"></A>        renderClassDescription( out, cls );
<A NAME="85"></A>        out.println();
<A NAME="86"></A>
<A NAME="87"></A>        <FONT ID="SingleLineComment">// sub-classes
<A NAME="88"></A></FONT>        <FONT ID="For">for</FONT> (Iterator i = cls.listSuperClasses( <FONT ID="True">true</FONT> ); i.hasNext(); ) {
<A NAME="89"></A>            out.print( <FONT ID="StringLiteral">"  is a sub-class of "</FONT> );
<A NAME="90"></A>            renderClassDescription( out, (OntClass) i.next() );
<A NAME="91"></A>            out.println();
<A NAME="92"></A>        }
<A NAME="93"></A>
<A NAME="94"></A>        <FONT ID="SingleLineComment">// super-classes
<A NAME="95"></A></FONT>        <FONT ID="For">for</FONT> (Iterator i = cls.listSubClasses( <FONT ID="True">true</FONT> ); i.hasNext(); ) {
<A NAME="96"></A>            out.print( <FONT ID="StringLiteral">"  is a super-class of "</FONT> );
<A NAME="97"></A>            renderClassDescription( out, (OntClass) i.next() );
<A NAME="98"></A>            out.println();
<A NAME="99"></A>        }
<A NAME="100"></A>    }
<A NAME="101"></A>
<A NAME="102"></A>    <FONT ID="FormalComment">/**
<A NAME="103"></A>     * &lt;p&gt;Render a description of the given class to the given output stream.&lt;/p&gt;
<A NAME="104"></A>     * @param out A print stream to write to
<A NAME="105"></A>     * @param c The class to render
<A NAME="106"></A>     */</FONT>
<A NAME="107"></A>    <FONT ID="Public">public</FONT> <FONT ID="Void">void</FONT> renderClassDescription( PrintStream out, OntClass c ) {
<A NAME="108"></A>        <FONT ID="If">if</FONT> (c.isUnionClass()) {
<A NAME="109"></A>            renderBooleanClass( out, <FONT ID="StringLiteral">"union"</FONT>, c.asUnionClass() );
<A NAME="110"></A>        }
<A NAME="111"></A>        <FONT ID="Else">else</FONT> <FONT ID="If">if</FONT> (c.isIntersectionClass()) {
<A NAME="112"></A>            renderBooleanClass( out, <FONT ID="StringLiteral">"intersection"</FONT>, c.asIntersectionClass() );
<A NAME="113"></A>        }
<A NAME="114"></A>        <FONT ID="Else">else</FONT> <FONT ID="If">if</FONT> (c.isComplementClass()) {
<A NAME="115"></A>            renderBooleanClass( out, <FONT ID="StringLiteral">"complement"</FONT>, c.asComplementClass() );
<A NAME="116"></A>        }
<A NAME="117"></A>        <FONT ID="Else">else</FONT> <FONT ID="If">if</FONT> (c.isRestriction()) {
<A NAME="118"></A>            renderRestriction( out, c.asRestriction() );
<A NAME="119"></A>        }
<A NAME="120"></A>        <FONT ID="Else">else</FONT> {
<A NAME="121"></A>            <FONT ID="If">if</FONT> (!c.isAnon()) {
<A NAME="122"></A>                out.print( <FONT ID="StringLiteral">"Class "</FONT> );
<A NAME="123"></A>                renderURI( out, prefixesFor( c ), c.getURI() );
<A NAME="124"></A>                out.print( <FONT ID="CharacerLiteral">' '</FONT> );
<A NAME="125"></A>            }
<A NAME="126"></A>            <FONT ID="Else">else</FONT> {
<A NAME="127"></A>                renderAnonymous( out, c, <FONT ID="StringLiteral">"class"</FONT> );
<A NAME="128"></A>            }
<A NAME="129"></A>        }
<A NAME="130"></A>    }
<A NAME="131"></A>
<A NAME="132"></A>
<A NAME="133"></A>    <FONT ID="SingleLineComment">// Internal implementation methods
<A NAME="134"></A></FONT>    <FONT ID="SingleLineComment">//////////////////////////////////
<A NAME="135"></A></FONT>
<A NAME="136"></A>    <FONT ID="FormalComment">/**
<A NAME="137"></A>     * &lt;p&gt;Handle the case of rendering a restriction.&lt;/p&gt;
<A NAME="138"></A>     * @param out The print stream to write to
<A NAME="139"></A>     * @param r The restriction to render
<A NAME="140"></A>     */</FONT>
<A NAME="141"></A>    <FONT ID="Protected">protected</FONT> <FONT ID="Void">void</FONT> renderRestriction( PrintStream out, Restriction r ) {
<A NAME="142"></A>        <FONT ID="If">if</FONT> (!r.isAnon()) {
<A NAME="143"></A>            out.print( <FONT ID="StringLiteral">"Restriction "</FONT> );
<A NAME="144"></A>            renderURI( out, prefixesFor( r ), r.getURI() );
<A NAME="145"></A>        }
<A NAME="146"></A>        <FONT ID="Else">else</FONT> {
<A NAME="147"></A>            renderAnonymous( out, r, <FONT ID="StringLiteral">"restriction"</FONT> );
<A NAME="148"></A>        }
<A NAME="149"></A>
<A NAME="150"></A>        out.println();
<A NAME="151"></A>
<A NAME="152"></A>        renderRestrictionElem( out, <FONT ID="StringLiteral">"    on property"</FONT>, r.getOnProperty() );
<A NAME="153"></A>        out.println();
<A NAME="154"></A>
<A NAME="155"></A>        <FONT ID="If">if</FONT> (r.isAllValuesFromRestriction()) {
<A NAME="156"></A>            renderRestrictionElem( out, <FONT ID="StringLiteral">"    all values from"</FONT>, r.asAllValuesFromRestriction().getAllValuesFrom() );
<A NAME="157"></A>        }
<A NAME="158"></A>        <FONT ID="If">if</FONT> (r.isSomeValuesFromRestriction()) {
<A NAME="159"></A>            renderRestrictionElem( out, <FONT ID="StringLiteral">"    some values from"</FONT>, r.asSomeValuesFromRestriction().getSomeValuesFrom() );
<A NAME="160"></A>        }
<A NAME="161"></A>        <FONT ID="If">if</FONT> (r.isHasValueRestriction()) {
<A NAME="162"></A>            renderRestrictionElem( out, <FONT ID="StringLiteral">"    has value"</FONT>, r.asHasValueRestriction().getHasValue() );
<A NAME="163"></A>        }
<A NAME="164"></A>    }
<A NAME="165"></A>
<A NAME="166"></A>    <FONT ID="Protected">protected</FONT> <FONT ID="Void">void</FONT> renderRestrictionElem( PrintStream out, String desc, RDFNode value ) {
<A NAME="167"></A>        out.print( desc );
<A NAME="168"></A>        out.print( <FONT ID="StringLiteral">" "</FONT> );
<A NAME="169"></A>        renderValue( out, value );
<A NAME="170"></A>    }
<A NAME="171"></A>
<A NAME="172"></A>    <FONT ID="Protected">protected</FONT> <FONT ID="Void">void</FONT> renderValue( PrintStream out, RDFNode value ) {
<A NAME="173"></A>        <FONT ID="If">if</FONT> (value.canAs( OntClass.<FONT ID="Class">class</FONT> )) {
<A NAME="174"></A>            renderClassDescription( out, (OntClass) value.as( OntClass.<FONT ID="Class">class</FONT> ) );
<A NAME="175"></A>        }
<A NAME="176"></A>        <FONT ID="Else">else</FONT> <FONT ID="If">if</FONT> (value <FONT ID="InstanceOf">instanceof</FONT> Resource) {
<A NAME="177"></A>            Resource r = (Resource) value;
<A NAME="178"></A>            <FONT ID="If">if</FONT> (r.isAnon()) {
<A NAME="179"></A>                renderAnonymous( out, r, <FONT ID="StringLiteral">"resource"</FONT> );
<A NAME="180"></A>            }
<A NAME="181"></A>            <FONT ID="Else">else</FONT> {
<A NAME="182"></A>                renderURI( out, r.getModel(), r.getURI() );
<A NAME="183"></A>            }
<A NAME="184"></A>        }
<A NAME="185"></A>        <FONT ID="Else">else</FONT> <FONT ID="If">if</FONT> (value <FONT ID="InstanceOf">instanceof</FONT> Literal) {
<A NAME="186"></A>            out.print( ((Literal) value).getLexicalForm() );
<A NAME="187"></A>        }
<A NAME="188"></A>        <FONT ID="Else">else</FONT> {
<A NAME="189"></A>            out.print( value );
<A NAME="190"></A>        }
<A NAME="191"></A>    }
<A NAME="192"></A>
<A NAME="193"></A>    <FONT ID="Protected">protected</FONT> <FONT ID="Void">void</FONT> renderURI( PrintStream out, PrefixMapping prefixes, String uri ) {
<A NAME="194"></A>        out.print( prefixes.shortForm( uri ) );
<A NAME="195"></A>    }
<A NAME="196"></A>
<A NAME="197"></A>    <FONT ID="Protected">protected</FONT> PrefixMapping prefixesFor( Resource n ) {
<A NAME="198"></A>        <FONT ID="Return">return</FONT> n.getModel().getGraph().getPrefixMapping();
<A NAME="199"></A>    }
<A NAME="200"></A>
<A NAME="201"></A>    <FONT ID="Protected">protected</FONT> <FONT ID="Void">void</FONT> renderAnonymous( PrintStream out, Resource anon, String name ) {
<A NAME="202"></A>        String anonID = (String) m_anonIDs.get( anon.getId() );
<A NAME="203"></A>        <FONT ID="If">if</FONT> (anonID == <FONT ID="Null">null</FONT>) {
<A NAME="204"></A>            anonID = <FONT ID="StringLiteral">"a-"</FONT> + m_anonCount++;
<A NAME="205"></A>            m_anonIDs.put( anon.getId(), anonID );
<A NAME="206"></A>        }
<A NAME="207"></A>
<A NAME="208"></A>        out.print( <FONT ID="StringLiteral">"Anonymous "</FONT>);
<A NAME="209"></A>        out.print( name );
<A NAME="210"></A>        out.print( <FONT ID="StringLiteral">" with ID "</FONT> );
<A NAME="211"></A>        out.print( anonID );
<A NAME="212"></A>    }
<A NAME="213"></A>
<A NAME="214"></A>    <FONT ID="Protected">protected</FONT> <FONT ID="Void">void</FONT> renderBooleanClass( PrintStream out, String op, BooleanClassDescription boolClass ) {
<A NAME="215"></A>        out.print( op );
<A NAME="216"></A>        out.println( <FONT ID="StringLiteral">" of {"</FONT> );
<A NAME="217"></A>
<A NAME="218"></A>        <FONT ID="For">for</FONT> (Iterator i = boolClass.listOperands(); i.hasNext(); ) {
<A NAME="219"></A>            out.print( <FONT ID="StringLiteral">"      "</FONT> );
<A NAME="220"></A>            renderClassDescription( out, (OntClass) i.next() );
<A NAME="221"></A>            out.println();
<A NAME="222"></A>        }
<A NAME="223"></A>        out.print( <FONT ID="StringLiteral">"  } "</FONT> );
<A NAME="224"></A>    }
<A NAME="225"></A>
<A NAME="226"></A>
<A NAME="227"></A>    <FONT ID="SingleLineComment">//==============================================================================
<A NAME="228"></A></FONT>    <FONT ID="SingleLineComment">// Inner class definitions
<A NAME="229"></A></FONT>    <FONT ID="SingleLineComment">//==============================================================================
<A NAME="230"></A></FONT>
<A NAME="231"></A>}
<A NAME="232"></A>
<A NAME="233"></A>
<A NAME="234"></A><FONT ID="MultiLineComment">/*
<A NAME="235"></A>    (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
<A NAME="236"></A>    All rights reserved.
<A NAME="237"></A>
<A NAME="238"></A>    Redistribution and use in source and binary forms, with or without
<A NAME="239"></A>    modification, are permitted provided that the following conditions
<A NAME="240"></A>    are met:
<A NAME="241"></A>
<A NAME="242"></A>    1. Redistributions of source code must retain the above copyright
<A NAME="243"></A>       notice, this list of conditions and the following disclaimer.
<A NAME="244"></A>
<A NAME="245"></A>    2. Redistributions in binary form must reproduce the above copyright
<A NAME="246"></A>       notice, this list of conditions and the following disclaimer in the
<A NAME="247"></A>       documentation and/or other materials provided with the distribution.
<A NAME="248"></A>
<A NAME="249"></A>    3. The name of the author may not be used to endorse or promote products
<A NAME="250"></A>       derived from this software without specific prior written permission.
<A NAME="251"></A>
<A NAME="252"></A>    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
<A NAME="253"></A>    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
<A NAME="254"></A>    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
<A NAME="255"></A>    IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
<A NAME="256"></A>    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
<A NAME="257"></A>    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
<A NAME="258"></A>    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
<A NAME="259"></A>    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
<A NAME="260"></A>    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
<A NAME="261"></A>    THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
<A NAME="262"></A>*/</FONT>
<A NAME="263"></A>
<A NAME="264"></A></pre>
</BODY>
</HTML>